package com.ruoyi.bantong.controller;

import java.util.List;
import java.util.Map;

import com.ruoyi.common.exception.CustomException;
import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.bantong.domain.BtPlacecardApply;
import com.ruoyi.bantong.service.IBtPlacecardApplyService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 版通申请Controller
 * 
 * @author laohei
 * @date 2021-01-26
 */
@RestController
@RequestMapping("/bantong/apply")
public class BtPlacecardApplyController extends BaseController
{
    @Autowired
    private IBtPlacecardApplyService btPlacecardApplyService;

    /**
     * 查询版通申请列表
     */
    //@PreAuthorize("@ss.hasPermi('bantong:apply:list')")
    @GetMapping("/list")
    public TableDataInfo list(BtPlacecardApply btPlacecardApply)
    {
        startPage();
        List<BtPlacecardApply> list = btPlacecardApplyService.selectBtPlacecardApplyList(btPlacecardApply);
        return getDataTable(list);
    }


    /**
     * 查询版通申请列表
     */
    //@PreAuthorize("@ss.hasPermi('bantong:apply:list')")
    @GetMapping("/bannerlist")
    public TableDataInfo bannerlist()
    {
        List<Map<String, Object>> list = btPlacecardApplyService.selectBtPlacecardApplyListByStatus();
        return getDataTable(list);
    }

    /**
     * 导出版通申请列表
     */
    //@PreAuthorize("@ss.hasPermi('bantong:apply:export')")
    @Log(title = "版通申请", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(BtPlacecardApply btPlacecardApply)
    {
        List<BtPlacecardApply> list = btPlacecardApplyService.selectBtPlacecardApplyList(btPlacecardApply);
        ExcelUtil<BtPlacecardApply> util = new ExcelUtil<BtPlacecardApply>(BtPlacecardApply.class);
        return util.exportExcel(list, "apply");
    }

    /**
     * 获取版通申请详细信息
     */
    //@PreAuthorize("@ss.hasPermi('bantong:apply:query')")
    @GetMapping(value = "/{paId}")
    public AjaxResult getInfo(@PathVariable("paId") Integer paId)
    {
        return AjaxResult.success(btPlacecardApplyService.selectBtPlacecardApplyById(paId));
    }

    /**
     * 新增版通申请
     */
    //@PreAuthorize("@ss.hasPermi('bantong:apply:add')")
    @Log(title = "版通申请", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BtPlacecardApply btPlacecardApply)
    {
        Integer btUserId = SecurityUtils.getBtUserId();
        btPlacecardApply.setUserId(btUserId);
        return toAjax(btPlacecardApplyService.insertBtPlacecardApply(btPlacecardApply));
    }

    /**
     * 修改版通申请  只有审核使用
     */
    //@PreAuthorize("@ss.hasPermi('bantong:apply:edit')")
    @Log(title = "版通申请", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BtPlacecardApply btPlacecardApply)
    {
        try {
            return toAjax(btPlacecardApplyService.updateBtPlacecardApply(btPlacecardApply));
        } catch (Exception e) {
            e.printStackTrace();
            if (e instanceof CustomException){
                return AjaxResult.error(e.getMessage());
            }else {
                return AjaxResult.error();
            }
        }
    }

    /**
     * 修改版通申请  前台使用
     */
    //@PreAuthorize("@ss.hasPermi('bantong:apply:edit')")
    @Log(title = "版通申请", businessType = BusinessType.UPDATE)
    @PutMapping("/edit")
    public AjaxResult editapi(BtPlacecardApply btPlacecardApply)
    {
        try {
            btPlacecardApply.setUserId(SecurityUtils.getBtUserId());
            return toAjax(btPlacecardApplyService.updateBtPlacecardApply(btPlacecardApply));
        } catch (Exception e) {
            if (e instanceof CustomException){
                return AjaxResult.error(e.getMessage());
            }else {
                return AjaxResult.error();
            }
        }
    }

    /**
     * 删除版通申请
     */
    //@PreAuthorize("@ss.hasPermi('bantong:apply:remove')")
    @Log(title = "版通申请", businessType = BusinessType.DELETE)
	@DeleteMapping("/{paIds}")
    public AjaxResult remove(@PathVariable Integer[] paIds)
    {
        return toAjax(btPlacecardApplyService.deleteBtPlacecardApplyByIds(paIds));
    }
}
